﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditItemTemplate : BaseDataVisualizer, ISingleItemDataVisualizer
    {
        public override void SetVisualizedData(DataVisualizer vis, object o) { ItemTemplateObject = o as ItemTemplate; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(ItemTemplate); } }
        public string VisualizerName { get { return "EditItemTemplate"; } }

        public static readonly DependencyProperty ItemTemplateObjectProperty = DependencyProperty.Register(
            "ItemTemplateObject", typeof(ItemTemplate), typeof(EditItemTemplate), new PropertyMetadata(null, new PropertyChangedCallback(ItemTemplateChanged)));

        public ItemTemplate ItemTemplateObject
        {
            get { return (ItemTemplate)GetValue(ItemTemplateObjectProperty); }
            set { SetValue(ItemTemplateObjectProperty, value); }
        }

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<EditItemTemplate, T2> action)
        {
            d.HandleChange<EditItemTemplate, T2>(e, action, (t) => t.UpdateBindings());
        }

        private static void ItemTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandleChange<ItemTemplate>(d, e, (t, val) => t.ItemTemplateObject = val);
        }

        public override void UpdateBindings()
        {
            if (gridData != null)
                gridData.DataContext = ItemTemplateObject;
        }

        public override string TitleText
        {
            get
            {
                if (ItemTemplateObject == null)
                    return string.Empty;

                return string.Format("Item Template {0} ({1} Props)", ItemTemplateObject.FriendlyName, ItemTemplateObject.PropertiesCount);
            }
        }

        public EditItemTemplate()
        {
            InitializeComponent();
        }

        private void basicTemplate_Loaded(object sender, RoutedEventArgs e)
        {
            //editBasicTemplate.wc.Indent = wc.Indent + 1;
        }

        public override void OnSaveData()
        {
            DataAccess.Client.StoreItemTemplateAsync(DataAccess.AssignedUniqueId, ItemTemplateObject);
        }

        private void btnCreateItem_Click(object sender, RoutedEventArgs e)
        {
            DataAccess.CreateItemCompleted += new EventHandler<ObjectEventArgsNonRef<long>>(DataAccess_CreateItemCompleted);
            DataAccess.Client.CreateItemFromTemplateAsync(DataAccess.AssignedUniqueId, DataAccess.CurrentlyPlaying.AssociatedID, ItemTemplateObject.AssociatedID);
        }

        void DataAccess_CreateItemCompleted(object sender, ObjectEventArgsNonRef<long> e)
        {
            // TODO
            DataAccess.CreateItemCompleted -= DataAccess_CreateItemCompleted;
        }
    }
}
